package com.example.cookieandsession.session;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * ClassName: OtherController
 * Package: com.example.cookieandsession.session
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-04-12
 * Time: 22:28
 */

@RestController
@RequestMapping("/app")
public class OtherController {

    @GetMapping("/lil")
    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //从request中 读取user属性
        String user= request.getParameter("user");
        //注意 是读不到的 因为httpServletRequest存储的属性 不能跨域请求
        //两个请求是没有关系的 HTTP协议是无状态协议

        //但是这两次请求都是在一个会话当中
        //读数据 也是要获取到Session
        HttpSession session = request.getSession(false);

        if (session != null){
            String username = (String) session.getAttribute("USERNAME");
            response.getWriter().println("Session: " + username);
        }


        //读不到
        response.getWriter().println("OtherServlet: " + user);



    }

}
